package koction.model.chatting;

import java.util.ArrayList;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@Service
public class ChattingServiceImpl implements ChattingService {
	ArrayList<Message> messageList=new ArrayList<Message>();
	
	/* (non-Javadoc)
	 * @see koction.model.chatting.ChattingService#sendMessage(koction.model.chatting.Message)
	 */
	@Override
	public synchronized void sendMessage(Message message){
		Message msg=new Message();
		msg.setUserId(message.getUserId());
		msg.setMessageNo(messageList.size()+1);
		msg.setContent(message.getContent());
		messageList.add(msg);
	}
	
	/* (non-Javadoc)
	 * @see koction.model.chatting.ChattingService#receiveMessage(int)
	 */
	@Override
	public ArrayList<Message> receiveMessage(int messageNo){
		ArrayList<Message> list=new ArrayList<Message>();
		if(messageList!=null&&!messageList.isEmpty()){
			for (int i=messageNo;i<messageList.size();i++){
				list.add(messageList.get(i));
			}
		}
		return list;
	}
	
	/* (non-Javadoc)
	 * @see koction.model.chatting.ChattingService#startChat(koction.model.chatting.Message)
	 */
	@Override
	public synchronized int startChat(Message message){
		Message msg=new Message();
		msg.setUserId(message.getUserId());
		msg.setMessageNo(messageList.size()+1);
		msg.setContent(message.getUserId()+" 님이 입장하셨습니다.");
		msg.setLog("true");
		messageList.add(msg);		
		return messageList.size()-1;
	}

	/* (non-Javadoc)
	 * @see koction.model.chatting.ChattingService#endChat(koction.model.chatting.Message)
	 */
	@Override
	public synchronized void endChat(Message message) {
		Message msg=new Message();
		msg.setUserId(message.getUserId());
		msg.setMessageNo(messageList.size()+1);
		msg.setContent(message.getUserId()+" 님이 퇴장하셨습니다.");
		msg.setLog("true");
		messageList.add(msg);			
	}
}
